using System;
using System.Collections.Generic;
using System.Linq;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Logic.Wizards;
using FireStarterModeller.Test.BO;
using FireStarterModeller.Test.BO.Fakes;
using Habanero.Base;
using Habanero.BO;
using Habanero.Testability;
using NUnit.Framework;
using Rhino.Mocks;
using Habanero.Testability.Testers;
namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestDMClassDeleter
    {
        #region Setup/Teardown

        [SetUp]
        public virtual void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [TearDown]
        public virtual void TearDownTest()
        {
        }

        #endregion

        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            TestUtilsLogic.SetupFixture();
        }

        [Test]
        public void Test_Construct_WhenClassNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new DMClassDeleter(null);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("dmClass", ex.ParamName);
            }
        }

        [Test]
        public void Test_Construct_ShouldSetClassDM()
        {
            //---------------Set up test pack-------------------
            var mockDMClass = MockRepository.GenerateMock<DMClass>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dmClassDeleter = new DMClassDeleter(mockDMClass);
            //---------------Test Result -----------------------
            Assert.AreSame(mockDMClass, dmClassDeleter.ClassDM);
        }

        [Test]
        public void Test_Delete_WhenNoRelationshipsAndNoLookups_ShouldMarkForDelete()
        {
            //If the Class is not involved in any Relationship and is not 
            // being used in Any Lookup Lists then it should be deleted.
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsEmpty(dmClass.RelationshipsDM);
            Assert.IsEmpty(dmClass.DMLookupListBusinessObjects);
            dmClass.AssertWasNotCalled(@class => @class.MarkForDelete());
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            dmClass.AssertWasCalled(@class => @class.MarkForDelete());
        }

        //This occurs because the ClassDef.XML has DeleteRelated Set to true
        [Test]
        public void Test_Delete_WhenHasRelationshipsAndNoLookups_ShouldMarkForDeleteAndMarkRelsForDelete()
        {
            //If the Class is involved in any Relationship and is not 
            // being used in Any Lookup Lists then it should be deleted along with its Relationships.
            //---------------Set up test pack-------------------
            IDMClass dmClass = new DMClass();
            var relationship = new DMRelationship();
            dmClass.RelationshipsDM.Add(relationship);
            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsNotEmpty(dmClass.RelationshipsDM);
            Assert.IsEmpty(dmClass.DMLookupListBusinessObjects);
            Assert.IsFalse(relationship.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(dmClass.Status.IsDeleted);
            Assert.IsTrue(relationship.Status.IsDeleted);
        }

        //This occurs because the ClassDef.XML has DeleteRelated Set to true
        [Test]
        public void Test_Delete_WhenHasRelationshipsAndHasLookups_ShouldMarkLookupsForDelete()
        {
            //If the Class is involved in any Relationship and is not 
            // being used in Any Lookup Lists then it should be deleted along with its Relationships.
            //---------------Set up test pack-------------------
            IDMClass dmClass = new DMClass();
            var relationship = new DMRelationship();
            dmClass.RelationshipsDM.Add(relationship);

            var dmLookupListBusinessObject = new DMLookupListBusinessObject();
            dmClass.DMLookupListBusinessObjects.Add(dmLookupListBusinessObject);

            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsNotEmpty(dmClass.RelationshipsDM);
            Assert.IsNotEmpty(dmClass.DMLookupListBusinessObjects);
            Assert.IsFalse(dmLookupListBusinessObject.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(dmLookupListBusinessObject.Status.IsDeleted);
        }

        [Test]
        public void Test_AllRelationshipWithRelatedFKs_WhenHasOne_ShouldReturnRel()
        {
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            DMRelationship mockRel = GetMockRel(true);
            dmClass.RelationshipsDM.Add(mockRel);
            var mockRelRelBOHasFKFalse = GetMockRel(false);
            dmClass.RelationshipsDM.Add(mockRelRelBOHasFKFalse);
            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsTrue(mockRel.RelatedBOHasForeignKey);
            Assert.IsFalse(mockRelRelBOHasFKFalse.RelatedBOHasForeignKey);
            //---------------Execute Test ----------------------
            var allRelationshipWithRelatedFKs = dmClassDeleter.AllRelationshipWithRelatedFKs;
            //---------------Test Result -----------------------
            Assert.AreEqual(1, allRelationshipWithRelatedFKs.Count());
            Assert.Contains(mockRel, allRelationshipWithRelatedFKs.ToList());
        }

        private static DMRelationship GetMockRel(bool relatedBOHasFK)
        {
            var mockRel = MockRepository.GenerateMock<DMRelationship>();
            mockRel.Stub(relationship => relationship.RelatedBOHasForeignKey).Return(relatedBOHasFK);
            return mockRel;
        }


        [Test]
        public void Test_AddRelationshipWithFKToBeDeleted()
        {
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);
            var dmRelationship = MockRepository.GenerateMock<IDMRelationship>();

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClassDeleter.RelationshipsWithFKToBeDeleted.Count());
            //---------------Execute Test ----------------------
            dmClassDeleter.AddRelationshipWithFKToBeDeleted(dmRelationship);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, dmClassDeleter.RelationshipsWithFKToBeDeleted.Count());
            Assert.AreSame(dmRelationship, dmClassDeleter.RelationshipsWithFKToBeDeleted.First());
        }

        [Test]
        public void Test_Delete_WhenHasRelationshipWithRelatedBOFK_AndFKPropSelectedToDelete_ShouldDeleteFKProp()
        {
            //If when deleting the DMClass the user selected to Delete the Related FKProp
            // the the Related FKProp Should Be Deleted
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);

            var dmRelationship = new FakeDMRelationship();
            dmRelationship.SetRelationshipProperties(new FakeBusinessObjectCollection<DMRelationshipProperty>());

            var relationshipProperty = new DMRelationshipProperty();
            var relatedProperty = new DMProperty();
            relationshipProperty.RelatedProperty = relatedProperty;
            dmRelationship.RelationshipProperties.Add(relationshipProperty);

            dmClassDeleter.AddRelationshipWithFKToBeDeleted(dmRelationship);
            //---------------Assert Precondition----------------
            Assert.AreSame(dmRelationship, dmClassDeleter.RelationshipsWithFKToBeDeleted.First());
            Assert.AreSame(relationshipProperty, dmRelationship.RelationshipProperties.First());
            Assert.IsFalse(relationshipProperty.Status.IsDeleted);
            Assert.IsFalse(relatedProperty.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(relationshipProperty.Status.IsDeleted);
            Assert.IsTrue(relatedProperty.Status.IsDeleted);
        }

        [Test]
        public void Test_Delete_WhenHasTwoRelationshipWithRelatedBOFK_AndFKPropSelectedToDelete_ShouldDeleteFKProp()
        {
            //If when deleting the DMClass the user selected to Delete the Related FKProp
            // the the Related FKProp Should Be Deleted
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);

            FakeDMRelationship dmRelationship = GetFakeRelationship();
            FakeDMRelationshipProperty relationshipProperty = AddRelProp(dmRelationship);
            FakeDMRelationship dmRelationship2 = GetFakeRelationship();
            FakeDMRelationshipProperty relationshipProperty2 = AddRelProp(dmRelationship2);

            dmClassDeleter.AddRelationshipWithFKToBeDeleted(dmRelationship);
            dmClassDeleter.AddRelationshipWithFKToBeDeleted(dmRelationship2);
            //---------------Assert Precondition----------------
            Assert.AreSame(dmRelationship, dmClassDeleter.RelationshipsWithFKToBeDeleted.First());
            Assert.AreSame(relationshipProperty, dmRelationship.RelationshipProperties.First());
            Assert.IsFalse(relationshipProperty.Status.IsDeleted);
            Assert.AreEqual(2, dmClassDeleter.RelationshipsWithFKToBeDeleted.Count);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(relationshipProperty.Status.IsDeleted);
            Assert.IsTrue(relationshipProperty2.Status.IsDeleted);
        }

        private static FakeDMRelationshipProperty AddRelProp(FakeDMRelationship dmRelationship)
        {
            var relationshipProperty = new FakeDMRelationshipProperty();
            if (dmRelationship.OwnerClass != null)
            {
                var property = dmRelationship.OwnerClass.Properties.CreateBusinessObject();
                relationshipProperty.Property = property;
            }
            dmRelationship.RelationshipProperties.Add(relationshipProperty);
            return relationshipProperty;
        }

        private static FakeDMRelationship GetFakeRelationship()
        {
            var dmRelationship = new FakeDMRelationship();
            dmRelationship.SetRelationshipProperties(new FakeBusinessObjectCollection<DMRelationshipProperty>());
            return dmRelationship;
        }

        [Test]
        public void Test_AllBOLookupsWithPropsToBeDeleted_WhenHasOne_ShouldReturnRel()
        {
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();

            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClassDeleter.AllLookupLists.Count());
            //---------------Execute Test ----------------------
            dmClass.DMLookupListBusinessObjects.Add(new DMLookupListBusinessObject());
            var allLookupLists = dmClassDeleter.AllLookupLists;
            //---------------Test Result -----------------------
            Assert.AreEqual(1, allLookupLists.Count());
        }

        [Test]
        public void Test_AddBOLListWithLookupListPropsToBeDeleted()
        {
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);
            var lookupList = new DMLookupListBusinessObject();

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClassDeleter.LookupListsWithPropsToBeDeleted.Count());
            //---------------Execute Test ----------------------
            dmClassDeleter.AddLookupListsWithPropsToBeDeleted(lookupList);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, dmClassDeleter.LookupListsWithPropsToBeDeleted.Count());
            Assert.AreSame(lookupList, dmClassDeleter.LookupListsWithPropsToBeDeleted.First());
        }

        [Test]
        public void Test_Delete_WhenHasLookupLists_ShouldDeleteLookupProp()
        {
            //If when deleting the DMClass the user selected to Delete the Related FKProp
            // the the Related FKProp Should Be Deleted
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);


            var dmProperty = new DMProperty();
            var lookupList = new DMLookupListBusinessObject {DMProperty = dmProperty};
            dmClassDeleter.AddLookupListsWithPropsToBeDeleted(lookupList);
            //---------------Assert Precondition----------------
            Assert.AreSame(lookupList, dmClassDeleter.LookupListsWithPropsToBeDeleted.First());
            Assert.AreSame(dmProperty, lookupList.DMProperty);
            Assert.IsFalse(lookupList.Status.IsDeleted);
            Assert.IsFalse(dmProperty.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(lookupList.Status.IsDeleted);
            Assert.IsTrue(dmProperty.Status.IsDeleted);
        }

        [Test]
        public void Test_Delete_WhenHasTwoLookupListsWithPropsToDelete_ShouldDeleteFKProp()
        {
            //If when deleting the DMClass the user selected to Delete the Related FKProp
            // the the Related FKProp Should Be Deleted
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);


            var dmProperty = new DMProperty();
            var lookupList = new DMLookupListBusinessObject {DMProperty = dmProperty};
            dmClassDeleter.AddLookupListsWithPropsToBeDeleted(lookupList);

            var dmProperty2 = new DMProperty();
            var lookupList2 = new DMLookupListBusinessObject {DMProperty = dmProperty2};
            dmClassDeleter.AddLookupListsWithPropsToBeDeleted(lookupList2);

            //---------------Assert Precondition----------------
            Assert.IsFalse(dmProperty.Status.IsDeleted);
            Assert.IsFalse(dmProperty2.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(dmProperty.Status.IsDeleted);
            Assert.IsTrue(dmProperty2.Status.IsDeleted);
        }

        [Test]
        public void Test_Delete_WhenDMPropNull_ShouldNotThrowError()
        {
            //If when deleting the DMClass the user selected to Delete the Related FKProp
            // the the Related FKProp Should Be Deleted
            //---------------Set up test pack-------------------
            IDMClass dmClass = GetMockDMClass();
            var dmClassDeleter = new DMClassDeleter(dmClass);

            var lookupList2 = new DMLookupListBusinessObject {DMProperty = null};
            dmClassDeleter.AddLookupListsWithPropsToBeDeleted(lookupList2);

            //---------------Assert Precondition----------------
            Assert.IsNull(lookupList2.DMProperty);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(true, "Should not raise Error");
        }

        [Test]
        public void Test_MarkMarkAllLookupListsPropsToBeDeleted_ShouldMoveAllTosWithFKToBeDeleted()
        {
            //---------------Set up test pack-------------------
            IDMClass dmClass = new DMClass();
            var lookupList = new DMLookupListBusinessObject();
            lookupList.DMProperty = new DMProperty();
            dmClass.DMLookupListBusinessObjects.Add(lookupList);
            var lookupList2 = new DMLookupListBusinessObject();
            lookupList.DMProperty = new DMProperty();
            dmClass.DMLookupListBusinessObjects.Add(lookupList2);
            var dmClassDeleter = new DMClassDeleter(dmClass);

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClassDeleter.LookupListsWithPropsToBeDeleted.Count());
            Assert.AreEqual(2, dmClass.DMLookupListBusinessObjects.Count);
            Assert.AreEqual(2, dmClassDeleter.AllLookupLists.Count());
            //---------------Execute Test ----------------------
            dmClassDeleter.MarkAllLookupListsPropsToBeDeleted();
            //---------------Test Result -----------------------
            Assert.AreEqual(2, dmClassDeleter.LookupListsWithPropsToBeDeleted.Count());
            Assert.AreEqual(2, dmClass.DMLookupListBusinessObjects.Count);
            Assert.AreEqual(2, dmClassDeleter.AllLookupLists.Count());
        }

        [Test]
        public void Test_Delete_ShouldMarkAllUniqueConstraintsForDelete()
        {
            //---------------Set up test pack-------------------
            var ucProp = TestUtilsDMUniqueConstraintProperty.CreateUnsavedValidDMUniqueConstraintProperty();
            var dmClass = ucProp.UniqueConstraint.Class;
            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            Assert.IsNotNull(ucProp.Property);
            Assert.IsFalse(ucProp.Status.IsDeleted);
            Assert.IsFalse(ucProp.Property.Status.IsDeleted);
            Assert.IsFalse(ucProp.UniqueConstraint.Status.IsDeleted);
            //---------------Execute Test ----------------------
            dmClassDeleter.Delete();
            //---------------Test Result -----------------------
            Assert.IsTrue(ucProp.Status.IsDeleted);
            Assert.IsTrue(ucProp.Property.Status.IsDeleted);
            Assert.IsTrue(ucProp.UniqueConstraint.Status.IsDeleted);
        }

        [Test]
        public void Test_Accept_DeleteAll_ShouldMarkAllForDelete()
        {
            //---------------Set up test pack-------------------
            var ucProp = TestUtilsDMUniqueConstraintProperty.CreateUnsavedValidDMUniqueConstraintProperty();
            var dmClass = ucProp.UniqueConstraint.Class;
            var lookupList = new DMLookupListBusinessObject {DMProperty = new DMProperty()};
            dmClass.DMLookupListBusinessObjects.Add(lookupList);
            var lookupList2 = new DMLookupListBusinessObject {DMProperty = new DMProperty()};
            dmClass.DMLookupListBusinessObjects.Add(lookupList2);

            FakeDMRelationship dmRelationship = GetFakeRelationship();
            dmClass.RelationshipsDM.Add(dmRelationship);
            var relationshipProperty = AddRelProp(dmRelationship);
            FakeDMRelationship dmRelationship2 = GetFakeRelationship();
            dmClass.RelationshipsDM.Add(dmRelationship2);
            var relationshipProperty2 = AddRelProp(dmRelationship2);

            //---------------Assert Precondition----------------
            //--------------LookupList--------------------------
            Assert.IsFalse(lookupList.Status.IsDeleted);
            Assert.IsFalse(lookupList.DMProperty.Status.IsDeleted);
            Assert.IsFalse(lookupList2.Status.IsDeleted);
            Assert.IsFalse(lookupList2.DMProperty.Status.IsDeleted);
            //--------------Relationship------------------------
            Assert.AreEqual(2, dmClass.RelationshipsDM.Count);
            //--------------UCProp------------------------------
            Assert.IsFalse(ucProp.Status.IsDeleted);
            Assert.IsFalse(ucProp.Property.Status.IsDeleted);
            Assert.IsFalse(ucProp.UniqueConstraint.Status.IsDeleted);
            //---------------Execute Test ----------------------
            new DMClassDeleter(dmClass).DeleteAll();
            //---------------Test Result -----------------------
            //--------------LookupList--------------------------
            Assert.IsTrue(lookupList.Status.IsDeleted);
            Assert.IsTrue(lookupList.DMProperty.Status.IsDeleted);
            Assert.IsTrue(lookupList2.Status.IsDeleted);
            Assert.IsTrue(lookupList2.DMProperty.Status.IsDeleted);
            //--------------Relationship------------------------
            Assert.IsTrue(relationshipProperty.Status.IsDeleted);
            Assert.IsTrue(relationshipProperty2.Status.IsDeleted);
            //---------------UCPRop------------------------------
            Assert.IsTrue(ucProp.Status.IsDeleted);
            Assert.IsTrue(ucProp.Property.Status.IsDeleted);
            Assert.IsTrue(ucProp.UniqueConstraint.Status.IsDeleted);
        }

        [Test]
        public void Test_WhenDMPropHasUIProp_ShouldNotGiveError()
        {
            //---------------Set up test pack-------------------
            var dmClass = TestUtilsDMClass.GetFactory()
                .WithMany(@class => @class.Properties)
                .WithOutSingleRelationships()
                .CreateValidBusinessObject();
            var dmProperty = dmClass.Properties[0];
            var uiProp = dmProperty.UIProps.CreateBusinessObject();
            //---------------Assert Precondition----------------
            Assert.IsFalse(dmClass.Status.IsDeleted);
            Assert.IsFalse(dmProperty.Status.IsDeleted);
            Assert.IsFalse(uiProp.Status.IsDeleted);
            //---------------Execute Test ----------------------
            new DMClassDeleter(dmClass).DeleteAll();
            //---------------Test Result -----------------------
            Assert.IsTrue(dmClass.Status.IsDeleted);
            Assert.IsTrue(dmProperty.Status.IsDeleted);
            Assert.IsTrue(uiProp.Status.IsDeleted);
        }

        [Test]
        public void Test_Construct_InheritanceRelationshipsToBeDeleted_ShouldBeListOfInheritanceRelationships()
        {
            //---------------Set up test pack-------------------
            var dmClass = TestUtilsDMClass.GetFactory().CreateDefaultBusinessObject();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Test Result -----------------------
            var inheritanceRelationshipsToBeDeleted = dmClassDeleter.InheritanceRelationshipsToBeDeleted;
            Assert.IsNotNull(inheritanceRelationshipsToBeDeleted);
            Assert.IsInstanceOf<IList<DMInheritanceRelationship>>(inheritanceRelationshipsToBeDeleted);
        }

        [Test]
        public void Test_InheritanceRelationshipsToBeDeleted_WhenCalledTwice_ShouldReturnSameInstance()
        {
            //---------------Set up test pack-------------------
            var dmClass = TestUtilsDMClass.GetFactory().CreateDefaultBusinessObject();
            var dmClassDeleter = new DMClassDeleter(dmClass);
            var firstListOfInhertincanceRelationship = dmClassDeleter.InheritanceRelationshipsToBeDeleted;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(firstListOfInhertincanceRelationship);
            //---------------Execute Test ----------------------

            var secondListOfInhertincanceRelationship = dmClassDeleter.InheritanceRelationshipsToBeDeleted;
            //---------------Test Result -----------------------
            Assert.AreSame(firstListOfInhertincanceRelationship, secondListOfInhertincanceRelationship);
        }

        [Test]
        public void Test_AddInheritanceRelationshipsToBeDeleted_ShouldAddTo_InheritanceRelationshipsToBeDelete()
        {
            //---------------Set up test pack-------------------
            var dmClass = TestUtilsDMClass.GetFactory().CreateDefaultBusinessObject();
            var dmClassDeleter = new DMClassDeleter(dmClass);
            var inheritanceRelationshipToBeDeleted = TestUtilsDMInheritanceRelationship.CreateUnsavedDefaultDMInheritanceRelationship();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClassDeleter.InheritanceRelationshipsToBeDeleted.Count, "Should Be no items in ToBeDeleted List");
            //---------------Execute Test ----------------------
            dmClassDeleter.AddInheritanceRelationshipToBeDeleted(inheritanceRelationshipToBeDeleted);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, dmClassDeleter.InheritanceRelationshipsToBeDeleted.Count, "Should Be no items in ToBeDeleted List");
            dmClassDeleter.InheritanceRelationshipsToBeDeleted.ShouldContain(inheritanceRelationshipToBeDeleted);
        }

        [Test]
        public void Test_MarkAllSubClassInheritanceRelationshipsToBeDeleted_ShouldMarkAll()
        {
            //---------------Set up test pack-------------------
            var dmClass = TestUtilsDMClass.GetFactory()
                .WithOutSingleRelationships()
                .WithMany(class1 => class1.SubClassRelationships, 3)
                .CreateValidBusinessObject();
            var dmClassDeleter = new DMClassDeleter(dmClass);
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClassDeleter.InheritanceRelationshipsToBeDeleted.Count, "Should Be no items in ToBeDeleted List");
            Assert.IsNotEmpty(dmClass.SubClassRelationships, "Should have at least one sub class Relationship");
            //---------------Execute Test ----------------------
            dmClassDeleter.MarkAllSubClassInheritanceRelationshipsToBeDeleted();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmClass.SubClassRelationships.Count, dmClassDeleter.InheritanceRelationshipsToBeDeleted.Count, "Should Be 3 items in ToBeDeleted List");
            dmClassDeleter.InheritanceRelationshipsToBeDeleted.ShouldContain(dmClass.SubClassRelationships, "InheritanceRelationshipsToBeDeleted ShouldContain all the SubClasses Defined on the DMClass");
        }

        [Test]
        public void Test_DeleteAll_WhenHasInheritanceSubClass_ShouldDeleteSubClassInheritanceRelationship_FixBug1155()
        {
            //---------------Set up test pack-------------------
            var dmClass = TestUtilsDMClass.GetFactory()
                .WithOutSingleRelationships()
                .WithMany(class1 => class1.SubClassRelationships, 1)
                .CreateValidBusinessObject();
            var inheritanceRelationship = dmClass.SubClassRelationships[0];
            //---------------Assert Precondition----------------
            Assert.IsNotEmpty(dmClass.SubClassRelationships, "Should have at least one sub class Relationship");
            Assert.IsFalse(dmClass.Status.IsDeleted);
            Assert.IsFalse(inheritanceRelationship.Status.IsDeleted);
            //---------------Execute Test ----------------------
            new DMClassDeleter(dmClass).DeleteAll();
            //---------------Test Result -----------------------
            Assert.IsTrue(dmClass.Status.IsDeleted);
            Assert.IsTrue(inheritanceRelationship.Status.IsDeleted);
        }

        private static IDMClass GetMockDMClass()
        {
            var dmClass = MockRepository.GenerateMock<IDMClass>();
            dmClass.Stub(@class => @class.RelationshipsDM).Return(new FakeBusinessObjectCollection<DMRelationship>());
            dmClass.Stub(@class => @class.DMLookupListBusinessObjects).Return(
                new FakeBusinessObjectCollection<DMLookupListBusinessObject>());
            return dmClass;
        }
    }
}